//
//  TWPhotoViewController.m
//  minidai
//
//  Created by 谭伟 on 2017/4/27.
//  Copyright © 2017年 迷你金服. All rights reserved.
//车辆图片展示控制器

#import "TWPhotoViewController.h"
#import "MNDPhotoBrowserCell.h"
#import "SDWebImageManager.h"
#import "UIImageView+WebCache.h"
#define CollectionCellReuseID @"photoCell"
@interface TWPhotoViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
/* 页面label */
@property (strong, nonatomic) UILabel *textLabel;
@end

@implementation TWPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self.view addGestureRecognizer:[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImageForDismiss)]];
    [self setupUI];
}

#pragma mark -搭建界面

- (void)setupUI {
    //创建collectionview
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc]init];
    layout.minimumLineSpacing = 0;

    [layout setScrollDirection:UICollectionViewScrollDirectionHorizontal];
    UICollectionView *collectionView = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight) collectionViewLayout:layout];
    [collectionView registerNib:[UINib nibWithNibName:@"MNDPhotoBrowserCell" bundle:nil] forCellWithReuseIdentifier:CollectionCellReuseID];
    layout.itemSize = CGSizeMake(kScreenWidth, kScreenWidth);
    collectionView.showsHorizontalScrollIndicator = NO;
    collectionView.backgroundColor = [UIColor whiteColor];
    collectionView.pagingEnabled = YES;
    collectionView.delegate = self;
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    //创建翻页label
    self.textLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, kScreenHeight - 50, kScreenWidth, 20)];
    self.textLabel.textAlignment = NSTextAlignmentCenter;
    self.textLabel.textColor = [UIColor blackColor];
    self.textLabel.font = [UIFont systemFontOfSize:16];
    self.textLabel.text = [NSString stringWithFormat:@"1/%ld",(unsigned long)self.photoArray.count];
    [self.view addSubview:_textLabel];
    
}

#pragma mark -UICollectionViewDelegate,UICollectionViewDataSource

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {

    return self.photoArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    MNDPhotoBrowserCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CollectionCellReuseID forIndexPath:indexPath];
//    cell.photoImg = [UIImage imageWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:self.photoArray[indexPath.row]]]];
    [cell.photoImgView sd_setImageWithURL:[NSURL URLWithString:self.photoArray[indexPath.row]] placeholderImage:[UIImage imageNamed:@"place_holder_img"]];
    //点按手势退出控制器
    return cell;
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    NSInteger index = scrollView.contentOffset.x/kScreenWidth+0.5;
    self.textLabel.text = [NSString stringWithFormat:@"%ld/%ld",(long)index+1,(unsigned long)self.photoArray.count];
}

- (void)clickImageForDismiss {
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
